//
//  EaseBubbleView+Task.m
//  ChatDemo-UI2.0
//
//  Created by jiuzhou on 16/3/17.
//  Copyright © 2016年 jiuzhou. All rights reserved.
//

#import "EaseBubbleView+Task.h"

@implementation EaseBubbleView (Task)
#pragma mark - private

- (void)_setupTaskBubbleMarginConstraints
{
    NSLayoutConstraint *marginTopConstraint = [NSLayoutConstraint constraintWithItem:self.taskBackView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.margin.top];
    NSLayoutConstraint *marginBottomConstraint = [NSLayoutConstraint constraintWithItem:self.taskBackView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-self.margin.bottom];
    NSLayoutConstraint *marginLeftConstraint = [NSLayoutConstraint constraintWithItem:self.taskBackView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.margin.right];
    NSLayoutConstraint *marginRightConstraint = [NSLayoutConstraint constraintWithItem:self.taskBackView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.margin.left];
    
    [self.marginConstraints removeAllObjects];
    [self.marginConstraints addObject:marginTopConstraint];
    [self.marginConstraints addObject:marginBottomConstraint];
    [self.marginConstraints addObject:marginLeftConstraint];
    [self.marginConstraints addObject:marginRightConstraint];
    
    [self addConstraints:self.marginConstraints];
}

- (void)_setupTaskBubbleConstraints
{
    [self _setupTaskBubbleMarginConstraints];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:21]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgType attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.firstLineView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgTitle attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:21]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgContent attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgTitle attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.msgContent attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgReadMore attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.secondLineView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgReadMore attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:18]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgReadMore attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgType attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstLineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgTitle attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgTitle attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgContent attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgContent attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.secondLineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgReadMore attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgReadMore attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.taskBackView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
}

#pragma mark - public

- (void)setupTaskBubbleView
{
    self.taskBackView = [[UIView alloc] init];
    self.taskBackView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.backgroundImageView addSubview:self.taskBackView];
    
    self.msgType = [[UILabel alloc] init];
    self.msgType.translatesAutoresizingMaskIntoConstraints = NO;
    [self.taskBackView addSubview:self.msgType];
    
    self.firstLineView = [[UIView alloc] init];
    self.firstLineView.translatesAutoresizingMaskIntoConstraints = NO;
    self.firstLineView.backgroundColor = RGB(240, 240, 240);
    [self.taskBackView addSubview:self.firstLineView];
    
    self.msgTitle = [[UILabel alloc] init];
    self.msgTitle.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgTitle.font = BoldSystemFont(17);
    [self.taskBackView addSubview:self.msgTitle];
    
    self.msgContent = [[UILabel alloc] init];
    self.msgContent.translatesAutoresizingMaskIntoConstraints=NO;
    self.msgContent.textColor = [UIColor lightGrayColor];
    self.msgContent.font = systemFont(14);
    [self.msgContent setNumberOfLines:2];
    [self.taskBackView addSubview:self.msgContent];
    
    self.secondLineView = [[UIView alloc] init];
    self.secondLineView.translatesAutoresizingMaskIntoConstraints = NO;
    self.secondLineView.backgroundColor = RGB(240, 240, 240);
    [self.taskBackView addSubview:self.secondLineView];
    
    self.msgReadMore = [[UILabel alloc] init];
    self.msgReadMore.translatesAutoresizingMaskIntoConstraints = NO;
    self.msgReadMore.font = systemFont(13);
    [self.taskBackView addSubview:self.msgReadMore];

    [self _setupTaskBubbleConstraints];
}

- (void)updateTaskMargin:(UIEdgeInsets)margin
{
    if (_margin.top == margin.top && _margin.bottom == margin.bottom && _margin.left == margin.left && _margin.right == margin.right) {
        return;
    }
    _margin = margin;
    
    [self removeConstraints:self.marginConstraints];
    [self _setupTaskBubbleMarginConstraints];
}

@end
